<template>
  <p-switch
    :size="size"
    :checked="CheckBoxToBl(value)"
    @change="changeswitch"
    :disabled="disabled"
  >
    <span
      v-for="item of intData"
      :slot="item.value == '1' ? 'checkedChildren':'unCheckedChildren'"
      :key="item.value"
    >
      {{item.text}}
    </span>
  </p-switch>
</template>

<script>
  import {ajaxGetDictItems} from '@/api/api'
  export default {
    name: 'Fswitch',
    props: {
      dictCode: {
        type: String,
        default: ''
      },
      disabled: Boolean,
      value: Number | String,
      field: String,
      options: {
        type: Array,
        default: () => []
      },
      size: {
        type: String,
        default: 'default'
      },
    },
    computed: {
      intData() {
        return {...this.DictOptions}
      }

    },
    data() {
      return {
        DictOptions: []
      }
    },
    created() {
      //获取字典数据
      this.initDictData();
    },
    watch:{
      options: function(val){
        this.initDictData()
      },
      value (val) {}
    },
    methods: {
      initDictData() {
        if(this.options && this.options.length > 0){
          this.DictOptions = [...this.options]
        }else if (this.dictCode == '') {
          this.DictOptions = [{value:'1', text:'是'}, {value:'0', text:'否'}]
        }else {
          ajaxGetDictItems(this.dictCode, null).then((res) => {
            if (res.success) {
              this.DictOptions = res.result;
            }
          })
        }
      },
      changeswitch(val) {
        let t = null
        console.log(val)
        val ? t = 1 : t = 0
        console.log(t)
        const f = {
          [this.field]: t
        }
        this.$emit('callback', f)
      },
      CheckBoxToBl(val){
        if(typeof val == 'string') {
          let is = 0
          is = val == "0" ?  false :  true
          return is
        } else {
          return !!val
        }
      }
    }
  }
</script>
